package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.MathGameHomeGameItemData;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName MathGameHomeGvAdapter
 * @Description
 * @date 2016-9-1
 * @history 2016-9-1 author: description:
 */
public class MathGameHomeGvAdapter extends AdapterBase<MathGameHomeGameItemData> {
    private Context context;
    private boolean isStarShowed;

    public void setStarShowed(boolean starShowed) {
        isStarShowed = starShowed;
    }

    public MathGameHomeGvAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.math_game_home_grid_item, null);
        }
        RoundImageView img = ViewHolder.get(convertView, R.id.math_game_home_grid_item_img);
        TextView titleTv = ViewHolder.get(convertView, R.id.math_game_home_grid_item_title_tv);
        RatingBar starRb = ViewHolder.get(convertView, R.id.math_game_home_grid_item_star);

        MathGameHomeGameItemData mathGameHomeGameItemData = getList().get(position);
        if (mathGameHomeGameItemData == null) {
            return convertView;
        }
        String gamename = mathGameHomeGameItemData.getGamename();
        String gameimage = mathGameHomeGameItemData.getGameimage();
        String star = mathGameHomeGameItemData.getStar();

        Glide.with(context).load(gameimage)
                .dontAnimate()
                .placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img);
        if (!AsStrUtils.isEmpty(gamename)) {
            titleTv.setText(gamename);
        }

        starRb.setVisibility(isStarShowed?View.VISIBLE:View.GONE);

        if (!AsStrUtils.isEmpty(star)) {
            starRb.setRating(Integer.parseInt(star));
        }
        return convertView;

    }
}
